﻿

<Flags()> _
Public Enum Alignment
    None = 0

    ' Horizontal layouts
    Left = 1
    Right = 2
    HorizontalCenter = 4

    ' Vertical layouts
    Top = 8
    Bottom = 16
    VerticalCenter = 32

    ' Combinations
    TopLeft = Top Or Left
    TopRight = Top Or Right
    TopCenter = Top Or HorizontalCenter

    BottomLeft = Bottom Or Left
    BottomRight = Bottom Or Right
    BottomCenter = Bottom Or HorizontalCenter

    CenterLeft = VerticalCenter Or Left
    CenterRight = VerticalCenter Or Right
    Center = VerticalCenter Or HorizontalCenter
End Enum

Public Structure Layout

    Public ClientArea As Rectangle
    Public SafeArea As Rectangle

    Public Sub New(clientArea__1 As Rectangle, safeArea__2 As Rectangle)
        ClientArea = clientArea__1
        SafeArea = safeArea__2
    End Sub

    Public Sub New(clientArea As Rectangle)
        Me.New(clientArea, clientArea)
    End Sub

    Public Sub New(viewport As Viewport)
        ClientArea = New Rectangle(CInt(viewport.X), CInt(viewport.Y), CInt(viewport.Width), CInt(viewport.Height))
        SafeArea = viewport.TitleSafeArea
    End Sub

    Public Function Place(size As Vector2, horizontalMargin As Single, verticalMargine As Single, alignment As Alignment) As Vector2
        Dim rc As New Rectangle(0, 0, CInt(size.X), CInt(size.Y))
        rc = Place(rc, horizontalMargin, verticalMargine, alignment)
        Return New Vector2(rc.X, rc.Y)
    End Function

    Public Function Place(region As Rectangle, horizontalMargin As Single, verticalMargine As Single, alignment__1 As Alignment) As Rectangle
        ' Horizontal layout.
        If (alignment__1 And Alignment.Left) <> 0 Then
            region.X = ClientArea.X + CInt(ClientArea.Width * horizontalMargin)
        ElseIf (alignment__1 And Alignment.Right) <> 0 Then
            region.X = ClientArea.X + CInt(ClientArea.Width * (1.0F - horizontalMargin)) - region.Width
        ElseIf (alignment__1 And Alignment.HorizontalCenter) <> 0 Then
            region.X = CInt(ClientArea.X + (ClientArea.Width - region.Width) / 2 + CInt(horizontalMargin * ClientArea.Width))
            ' Don't do layout.
        Else
        End If

        ' Vertical layout.
        If (alignment__1 And Alignment.Top) <> 0 Then
            region.Y = ClientArea.Y + CInt(ClientArea.Height * verticalMargine)
        ElseIf (alignment__1 And Alignment.Bottom) <> 0 Then
            region.Y = ClientArea.Y + CInt(ClientArea.Height * (1.0F - verticalMargine)) - region.Height
        ElseIf (alignment__1 And Alignment.VerticalCenter) <> 0 Then
            region.Y = CInt(ClientArea.Y + (ClientArea.Height - region.Height) / 2 + CInt(verticalMargine * ClientArea.Height))
            ' Don't do layout.
        Else
        End If

        ' Make sure layout region is in the safe area.
        If region.Left < SafeArea.Left Then
            region.X = SafeArea.Left
        End If

        If region.Right > SafeArea.Right Then
            region.X = SafeArea.Right - region.Width
        End If

        If region.Top < SafeArea.Top Then
            region.Y = SafeArea.Top
        End If

        If region.Bottom > SafeArea.Bottom Then
            region.Y = SafeArea.Bottom - region.Height
        End If

        Return region
    End Function

End Structure